package mdm.led.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class NumUtil {
//	public static String formatNum(Date date) throws Exception {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        return simpleDateFormat.format(date);
//    }
//	public static void main(String[] args)
//	 {
//	  String str = "";
//	  String str1 = "";
//	  String s = "R88BR180001-01";
//	  for(int i = 1;i < 100;i++)
//	  {
//	     str = String.format("%04d", i);
//	     str1 = String.format("%02d", i);
////	     System.out.println(str);
//	     System.out.println(str1);
//	  }
//	  System.out.println(s.substring(0, s.indexOf("-")));
//	 }
	
	public static List<String> stringToLists(String strs){
    	String str[] = strs.split(",");
    	return Arrays.asList(str);
    }
	
	
	public static List<Integer> integerToLists(String strs){
//		List<String> idsStringList = Arrays.asList(ids.split(","));
//		List<Integer> idsList = new ArrayList<>();
//		CollectionUtils.collect(idsStringList, new Transformer() {
//		   @Override
//		   public Object transform(Object o) {
//			  return Integer.valueOf(o.toString());
//		   }
//		}, idsList);
		
//		String str[] = strs.split(",");
		List<Integer> ids =Arrays.stream(strs.split(",")).map(s->Integer.parseInt(s.trim())).
		         collect(Collectors.toList());
		return ids;
	}
	
	/**
	 * 只能是数字、字母、中文
	 * @param text
	 * @return
	 */
	public static boolean stringInput(String text){
//		char ch = text.charAt(text.length() - 1);
//		if (!(ch >= '0' && ch <= '9'
//            || ch >= 'A' && ch <= 'z' 
//                || ch >= '\u4E00' && ch <= '\u9FA5')) {
//			return true;
//		}
//		return false;
		Pattern pattern = Pattern.compile("^[a-z0-9A-Z\u4e00-\u9fa5]+$");
	    return !pattern.matcher(text).matches();   
	}
	
	/**
	 * 只能是数字和字母
	 * @param text
	 * @return
	 */
	public static boolean stringInputToString(String text){
//		char ch = text.charAt(text.length() - 1);
//		if (!(ch >= '0' && ch <= '9'
//				|| ch >= 'A' && ch <= 'z' )) {
//			return true;
//		}
//		return false;
		Pattern pattern = Pattern.compile("[a-zA-Z0-9]+");
	    return !pattern.matcher(text).matches();   
		
	}
	
	/**
	 * 只能是数字
	 * @param text
	 * @return
	 */
//	public static boolean stringInputToNum(String text){
//		char ch = text.charAt(text.length() - 1);
//		if (!(ch >= '0' && ch <= '9')) {
//			return true;
//		}
//		return false;
//	}
	public static boolean stringInputToNum(String str){
	    Pattern pattern = Pattern.compile("[0-9]*");
	    return !pattern.matcher(str).matches();   
	}
	
	/**
	 * 只能是字母
	 * @param text
	 * @return
	 */
	public static boolean stringInputToAbc(String text){
		char ch = text.charAt(text.length() - 1);
		if (!(ch >= 'A' && ch <= 'z')) {
			return true;
		}
		return false;
	}
	
	public static List<String> stringToArray(String strs){
    	String str[] = strs.split(",");
//    	String[] arr = str.split("");
  	   for(int i =0;i<str.length;i++){ //String数组，不过arr[0]为空
  		   System.out.println(str[i]);    //空  1 2 3 a b c
  	   }
    	return Arrays.asList(str);
    }
	
	public static void setBorderForMergeCell(int i, CellRangeAddress cellRangeTitle, HSSFSheet sheet, HSSFWorkbook workBook){
	        RegionUtil.setBorderBottom(i, cellRangeTitle, sheet, workBook);
	        RegionUtil.setBorderLeft(i, cellRangeTitle, sheet, workBook);
	        RegionUtil.setBorderRight(i, cellRangeTitle, sheet, workBook);
	        RegionUtil.setBorderTop(i, cellRangeTitle, sheet, workBook);
	    }
	
	//对象转换为List集合
//	public static <T> List<T> strToList(String str, Class<T> clazz) {
//        JSONArray json = JSONArray.fromObject(str);
//        JSONObject object = null;
//        T t = null;
//        List<T> list = new ArrayList<>();
//        for (int i = 0; i < json.size(); i++) {
//            object = JSONObject.fromObject(json.get(i));
//            t = (T) JSONObject.toBean(object, clazz);
//            list.add(t);
//        }
//        return list;
//    }

	public static boolean isValidDate(String str) {
	      boolean convertSuccess=true;
	      // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	       try {
	    	   // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
	          format.setLenient(false);
	          format.parse(str);
	       } catch (ParseException e) {
	          // e.printStackTrace();
	// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
	           convertSuccess=false;
	       } 
	       return convertSuccess;
	}
	
	//基姆拉尔森计算公式根据日期判断星期几
    public static void  CalculateWeekDay(int y, int m, int d) {
        if (m == 1 || m == 2) {
            m += 12;
            y--;
        }
        int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
        switch (iWeek) {
            case 0:
                System.out.printf("星期一\n");
                break;
            case 1:
                System.out.printf("星期二\n");
                break;
            case 2:
                System.out.printf("星期三\n");
                break;
            case 3:
                System.out.printf("星期四\n");
                break;
            case 4:
                System.out.printf("星期五\n");
                break;
            case 5:
                System.out.printf("星期六\n");
                break;
            case 6:
                System.out.printf("星期日\n");
                break;
        }
    }
	

}
